package com.dhh.sky.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import com.dhh.sky.SDAApplication;
import com.dhh.sky.util.Constant;
import com.dhh.sky.util.FileManager;
import com.dhh.sky.util.JsonKeys;
import com.microsoft.live.LiveConnectClient;
import com.microsoft.live.LiveOperation;
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class CameraUploadService extends IntentService {
	private SharedPreferences spTemp;
	private String parent;
	private FileManager fm;
	private LiveConnectClient mClient;
	public static boolean isStop = false;

	public CameraUploadService() {
		super(Constant.SERVICE_NAME_CAMERA);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		spTemp = getSharedPreferences(Constant.FOLDER_LOG_CAMERA_FOLDER_TEMP, 0);
		fm = new FileManager(this);
		mClient = ((SDAApplication) getApplication()).getConnectClient();
		parent = fm.getFileByName(Constant.CAMERA_UPLOAD_FOLDER).getId();
		isStop = false;
		upload();
	}
	
	
	public List<String> getPhotos(){
		List<String> names = new ArrayList<String>();
		Map valueMap = spTemp.getAll();
		Iterator it = valueMap.keySet().iterator();
		while(it.hasNext()){
			names.add(valueMap.get(it.next()).toString());
		}
		spTemp.edit().clear().commit();
		return names;
	}

	private void upload(){
		SharedPreferences sp = getSharedPreferences(Constant.FOLDER_LOG_CAMERA_FOLDER, 0);
		List<String> names = getPhotos();
        int current = 0;
        int max = names.size();        

		Log.d(Constant.TAG, "Start uploading photos....");
		Log.d(Constant.TAG, "Sum: "+max);
        
        for(String name : names){
        	File file = new File(name);
        	try {
            	sendProgress(current,max,file.getAbsolutePath(),null,false);
        		LiveOperation opration = mClient.upload(parent, file.getName(), file);
        		if(isStop){
        			break;
        		}else{
        			JSONObject result = opration.getResult();
            		if(result.has(JsonKeys.ERROR)){
            			sendProgress(current,max,file.getAbsolutePath(),file.getName(),true);
            			Log.e(Constant.TAG, result.getString(JsonKeys.ERROR));
            		}else{
            			sp.edit().putString(file.getAbsolutePath(), file.getAbsolutePath()).commit();
            		}
        		} 
        	}catch(Exception e){
        		Log.e(Constant.TAG, e.getMessage(), e);
        	}
        	current ++;
        	if(current>=max){
        		sendProgress(max,max,null,null,false);
        	}
        }        
	}

	private void sendProgress(int current,int max,String name,String message,boolean isFailed){
		Intent respone = new Intent(Constant.SERVICE_NAME_CAMERA);
		respone.putExtra(Constant.PARAM_ITEM_NAME, name);
		respone.putExtra(Constant.PARAM_MAX, max);
		respone.putExtra(Constant.PARAM_PROGRESS, current);
		respone.putExtra(Constant.PARAM_DONE, isFailed);
		if(message!=null)
			respone.putExtra(Constant.PARAM_MESSAGE, message);
		
		Log.d(Constant.TAG, "sendProgress: "+name);
		
		sendBroadcast(respone);
	}
}
